package cn.tedu.boot42.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class UploadController {
    @RequestMapping("/upload")
    public String upload(MultipartFile picFile) throws IOException {
        System.out.println("picFile"+picFile);
        String fileName=picFile.getOriginalFilename();
        String suffix= fileName.substring(fileName.lastIndexOf("."));
        fileName=UUID.randomUUID()+suffix;
        System.out.println(fileName);
        String dirPath="D:/files";
        File file=new File(dirPath);
        if (!file.exists()){
            file.mkdirs();
        }
        String filePath=dirPath+"/"+fileName;
        picFile.transferTo(new File(filePath));
        System.out.println("文件已添加");
        return fileName;
    }
    @RequestMapping("/remove")
    public void remove(String name){
        System.out.println("开始删除");
        String dirPath="D:/files";
        System.out.println(dirPath+"/"+name);
        String filePath=dirPath+"/"+name;
        System.out.println(filePath);
        new File(filePath).delete();
    }
}
